/**
 * 创建人:  @author huangpeng
 * 创建时间:  2024年10月13日 20:21
 * 项目名称:  lemon-blog-cloud
 * 文件名称:  IBlBlogApprovalServiceImpl
 * 文件描述:  @Description: 文章复核Service接口实现类
 * 公司名称:  xxxxxx
 * <p>
 * All rights Reserved, Designed By xxxxxx
 *
 * @Copyright: 2021-2024
 */
package com.lemon.blog.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.lemon.blog.system.domain.BlBlog;
import com.lemon.blog.system.mapper.BlBlogMapper;
import com.lemon.common.core.exception.ServiceException;
import com.lemon.common.core.utils.StringUtils;
import com.lemon.common.security.utils.OptEntityUtils;
import com.lemon.system.api.RemoteSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lemon.common.security.service.BaseServiceImpl;
import com.lemon.blog.system.mapper.BlBlogApprovalMapper;
import com.lemon.blog.system.domain.BlBlogApproval;
import com.lemon.blog.system.service.IBlBlogApprovalService;

/**
 * 包名称：com.lemon.blog.system.service.impl
 * 类名称：BlBlogApprovalServiceImpl
 * 类描述：文章复核Service业务层处理
 * 创建人：@author lemon
 * 创建时间：2024-10-13
 */
@Service
public class BlBlogApprovalServiceImpl extends BaseServiceImpl<BlBlogApproval> implements IBlBlogApprovalService {

    @Autowired
    private BlBlogApprovalMapper blBlogApprovalMapper;

    @Autowired
    private BlBlogMapper blBlogMapper;

    @Autowired
    private RemoteSearchService remoteSearchService;

    @Override
    public int insert(BlBlogApproval model) {
        if (CollUtil.isEmpty(model.getBlogIdList())) {
            throw new ServiceException("文章id列表不能为空");
        }
        if (StringUtils.isEmpty(model.getApprovalStatus())) {
            throw new ServiceException("审核状态不能为空");
        }

        List<BlBlogApproval> approvalList = new ArrayList<>();
        List<BlBlog> blogList = new ArrayList<>();
        for (String blogId : model.getBlogIdList()) {
            BlBlogApproval approval = new BlBlogApproval();
            approval.setBlogId(Long.parseLong(blogId));
            approval.setReplyContent(model.getReplyContent());
            approval.setApprovalStatus(model.getApprovalStatus());
            OptEntityUtils.preInsert(approval);
            approvalList.add(approval);

            BlBlog blBlog = new BlBlog();
            blBlog.setId(Long.parseLong(blogId));
            blBlog.setApprovalStatus(model.getApprovalStatus());
            OptEntityUtils.preUpdate(blBlog);
            blogList.add(blBlog);
        }

        blBlogMapper.updateBatchSelective(blogList);
        // 更新elasticsearch索引
        remoteSearchService.initElasticSearchIndex();
        return blBlogApprovalMapper.batchInsert(approvalList);
    }


    @Override
    public List<BlBlogApproval> selectBlBlogApprovalList(BlBlogApproval blBlogApproval) {
        return blBlogApprovalMapper.selectBlBlogApprovalList(blBlogApproval);
    }

}